<?php

defined('ACC')||exit('Access Denied');

class CatModel extends Model{
	
	protected $lists=array();
	
	/* 获取所有栏目信息 */
	public function getList(){
		$sql='select * from category order by cat_id asc';
		return $this->db->getAll($sql);
	}
	
	/* 获取所有栏目并排序 */
	public function catSort($arr,$parent_id=0,$lev=1){
		$this->lists=array();
		$this->catSort2($arr,$parent_id,$lev);
		return $this->lists;
	}
	
	/* 栏目排序 */
	public function catSort2($arr,$parent_id=0,$lev=1){
		foreach($arr as $v){
			if($v['parent_id']==$parent_id){
				$v['lev']=$lev;
				$this->lists[]=$v;
				$this->catSort2($arr,$v['cat_id'],$lev+1);
			}
				
		}
	}
	
	/* 添加栏目 */
	public function add($data){
		return $this->db->autoExecute($data,'category');
	}
	
	/* 删除一行栏目 */
	public function del($cat_id){
		$sql='delete from category where cat_id='.$cat_id;
		if($this->db->query($sql)){
			return $this->db->affected_rows();
		}
		return false;
	}
	
	/* 获取某栏目下所有子栏目 */
	public function getSon($cat_id){
		$sql='select * from category where parent_id='.$cat_id;
		return $this->db->getAll($sql);
	}
	
	/* 获取一行栏目所有信息 */
	public function info($cat_id){
		$sql='select * from category where cat_id='.$cat_id;
		return $this->db->getRow($sql);
	}
	
	/* 修改一样栏目 */
	public function up($data,$cat_id=-1){
		$where='cat_id='.$cat_id;
		return $this->db->autoExecute($data,'category','update',$where);
	}
	
	/* 找父类树 */
	public function getTree($list,$cat_id){
		static $arr=array();
		foreach($list as $v){
			if($v['cat_id']==$cat_id){
				$this->getTree($list,$v['parent_id']);
				$arr[]=$v;
			}
		}
		return $arr;
	}
	
	/* 获取一行栏目信息 */
	public function getRow($cat_id){
		$sql = "select * from category where cat_id = $cat_id";
		return $this->db->getRow($sql);
	}
	
	/* 获取所有栏目信息 */
	public function getListInfo(){
		$sql = 'select cat_id, cat_name, parent_id from category order by cat_id asc';
		return $this->db->getAll($sql);
	}
	
	/* 修改栏目的父类 */
	public function move($cat_id, $parent_id){
		$sql = 'update category set parent_id = ' . $parent_id . ' where cat_id = ' . $cat_id;
		return $this->db->query($sql);
	}
	
	/* 获取栏目列表 */
	public function get_cat_list(){
		$sql = 'select cat_id, cat_name from category where cat_id > 10';
		return $this->db->getAll($sql);
	}
	
	/* 获取某一栏目下所有属性 */
	public function get_cat_attrs($cat_id){
		$sql = 'select attr_id,attr_name from attribute where cat_id = ' . $cat_id;
		return $this->db->getAll($sql);
	}
	
}


?>